﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;

public partial class Control_complexGrid : System.Web.UI.UserControl
{
    protected string orderByString = string.Empty;

    System.Data.DataTable DT = null;

    [Description("設定顯示名"), Category("MISAPP"), DefaultValue("")]
    public string Caption
    {
        set
        {
            //lbl_Caption.Text = value;
            GV1.Caption = value;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    public void SetData(System.Data.DataTable p_dt)
    {
        if (p_dt is System.Data.DataTable)
        {
            DT = p_dt;
            GV1.DataSource = p_dt;
            GV1.DataBind();
        }        
    }

    protected void FillDDLSeq(DropDownList ddl)
    {
        for (int i = 0; i < DT.Rows.Count; i++)
        {
            ddl.Items.Add(new ListItem(i.ToString()));
        }
    }

    protected void GV1_ondatabound(object sender, EventArgs e)
    {
        Label lbl = null;
        int i = 0;
        System.Data.DataRow dtRow = null;        
        foreach (GridViewRow row in GV1.Rows)
        {
            dtRow = DT.Rows[i];
            lbl = row.FindControl("lblDisplay") as Label;
            lbl.Text = dtRow["fldname"].ToString();
            lbl = row.FindControl("lblValue") as Label;
            lbl.Text =  dtRow["fldvalue"].ToString();            
            DropDownList ddl = row.FindControl("DropDownList1") as DropDownList;
            FillDDLSeq(ddl);
            ddl = row.FindControl("ddl_OrderBy") as DropDownList;
            FillDDLSeq(ddl);
            i++;
        }
    }


    /// <summary>
    /// 取回所選的資料，傳回字串
    /// 2011-04-15 新加入一個排序條件，但是要有選群組的才能排。
    /// </summary>
    /// <returns></returns>
    public string GetSelect()
    {
        CheckBox chk = null;
        Label lbl = null;
        DropDownList ddl,ddlOrder;
        Dictionary<string, int> selects = new Dictionary<string, int>();
        Dictionary<string, int> selOrderBy = new Dictionary<string, int>();
        foreach (GridViewRow row in GV1.Rows)
        {
            chk = row.FindControl("CheckBox1") as CheckBox;
            if (chk.Checked)
            {
                lbl = row.FindControl("lblValue") as Label;
                ddl = row.FindControl("DropDownList1") as DropDownList;
                ddlOrder = row.FindControl("ddl_OrderBy") as DropDownList;
                selects.Add(lbl.Text
                            , Convert.ToInt16(ddl.SelectedValue)
                           );
                selOrderBy.Add(lbl.Text
                    ,Convert.ToInt16(ddlOrder.SelectedValue));
            }
        }
        orderByString = ParseDictionary(SortDictionary(selOrderBy));
        return ParseDictionary(SortDictionary(selects));
    }

    /// <summary>
    /// 取得排序，以有選Group 的才能排序
    /// </summary>
    /// <returns></returns>
    public string GetOrderByString()
    {
        GetSelect();
        return orderByString;
    }

    protected Dictionary<string, int> SortDictionary(Dictionary<string, int> p_sort)
    {
        Dictionary<string, int> sort = new Dictionary<string, int>();

        var items = from k in p_sort.Keys
                orderby p_sort[k] ascending
                select k;

        foreach (string itm in items)
        {
            sort.Add(itm, p_sort[itm]);    
        }
        return sort;
    }

    protected string ParseDictionary(Dictionary<string, int> p_groupBy)
    {
        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        foreach (KeyValuePair<string, int> itm in p_groupBy)
        {
            sb.AppendFormat(",{0}", itm.Key);
        }
        return sb.ToString().Length == 0 ? "" : sb.ToString().Substring(1);
    }
}
